package cc.rengu.igas.channel.nucc.core.service.trans;

import cc.rengu.igas.channel.nucc.common.constant.NuccParamConstant;
import cc.rengu.igas.channel.nucc.common.constant.NuccTreeNodeConstant;
import cc.rengu.igas.channel.nucc.common.enums.RespCodeEnum;
import cc.rengu.igas.channel.nucc.common.enums.TrxCtgyCdEnum;
import cc.rengu.igas.channel.nucc.facade.bean.*;
import cc.rengu.igas.channel.nucc.facade.request.MchntPaymentRequest;
import cc.rengu.igas.channel.nucc.facade.request.MchntPaymentAcqRequest;
import cc.rengu.igas.channel.nucc.facade.response.CommonTransAcqResponse;
import cc.rengu.igas.channel.nucc.facade.response.CommonTransResponse;
import cc.rengu.igas.channel.nucc.core.realize.impl.NuccSeqNoManageImpl;
import cc.rengu.igas.channel.nucc.core.realize.impl.NuccServiceImpl;
import cc.rengu.igas.channel.nucc.core.realize.NuccSeqNoManage;
import cc.rengu.igas.channel.nucc.core.realize.NuccService;
import cc.rengu.oltp.service.base.BizBaseService;
import cc.rengu.oltp.service.common.constant.TreeNodeConstant;
import cc.rengu.oltp.service.model.RiskMonitorInfo;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.utility.util.*;
import cc.rengu.utility.base.TimeUtil;

/**
 * 付款申请服务
 */
public class EpccMchntPaymentService extends BizBaseService {
    @Override
    protected Object beforeBizProcess() throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        MchntPaymentRequest request = new MchntPaymentRequest();
        ConvertUtil.convertOutput(request);

        xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.FRONT_SEQ_NUM, request.getHeader().getTraceNo());
        xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.MCHNT_ORDER_ID, request.getOrderId());
        xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.ORDER_CCY, request.getTrxAmt().substring(0, 3));
        xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.ORDER_AMT, request.getTrxAmt().substring(3));
        xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.ISS_INST_CODE, request.getPyerAcctIssrId());
        xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.PAY_ACCT_TYPE, request.getPyerAcctTp());
        xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.PAY_ACCT_INFO, request.getPyerAcctNo());
        return request;
    }

    @Override
    protected BizResponse callBizService(Object request) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        MchntPaymentRequest innerRequest = (MchntPaymentRequest) request;
        xmlTreeUtil.setXmlTreeStringValue(NuccTreeNodeConstant.EPCC_ISSR_ID, innerRequest.getInstgId());

        NuccService nuccService = new NuccServiceImpl();
        MchntPaymentAcqRequest acqRequest = initMchntPaymentAcqRequest(xmlTreeUtil, innerRequest);
        CommonTransAcqResponse acqResponse = nuccService.mchntPayment(acqRequest);
        BizResponse bizResponse = new BizResponse();
        bizResponse.setRspSysId(NuccParamConstant.IGAS_SYS_ID);
        if (!NuccParamConstant.EPCC_SUCCESS_CODE.equals(acqResponse.getSysRtnCd())) {
            rglog.error("调用网联接口失败，sysRtnCd=[{}],sysRtnDesc=[{}]", acqResponse.getSysRtnCd(), acqResponse.getSysRtnDesc());
            bizResponse.setRespCode(acqResponse.getSysRtnCd());
            bizResponse.setRespDesc(acqResponse.getSysRtnDesc());
        } else {
            /*将交易批次号保存到流水表中*/
            xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.TRANS_BATCH_NO, acqResponse.getBatchId());

            BizInfBean bizInf = new BizInfBean();
            bizInf.setDbtrBankId(xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.SYS_SEQ_NUM));

            CommonTransResponse response = new CommonTransResponse();
            response.setBizInf(bizInf);

            bizResponse.setRespCode(acqResponse.getBizStsCd());
            bizResponse.setRespDesc(acqResponse.getBizStsDesc());
            bizResponse.setResult(response);
        }

        return bizResponse;
    }

    @Override
    protected void afterBizProcess(BizResponse response) throws Exception {

    }

    @Override
    protected RiskMonitorInfo getRiskMonitorInfo() {
        return null;
    }

    private MchntPaymentAcqRequest initMchntPaymentAcqRequest(XmlTreeUtil xmlTreeUtil, MchntPaymentRequest innerRequest) throws Exception {
        MchntPaymentAcqRequest mchntPaymentAcqRequest = new MchntPaymentAcqRequest();

        PyerInfBean pyerInf = new PyerInfBean();
        pyerInf.setPyerAcctIssrId(innerRequest.getPyerAcctIssrId());
        pyerInf.setPyerAcctTp(innerRequest.getPyerAcctTp());
        pyerInf.setPyerAcctNo(innerRequest.getPyerAcctNo());
        pyerInf.setPyerAcctNm(innerRequest.getPyerAcctNm());
        /*对公户*/
        if ("05".equals(innerRequest.getPyerAcctTp())) {
            pyerInf.setMchntNo(innerRequest.getPyerMrchntNo());
            pyerInf.setMchntNm(innerRequest.getPyerMrchntNm());
            pyerInf.setMchntShrtNm(innerRequest.getPyerMrchntShrtNm());
        }
        mchntPaymentAcqRequest.setPyerInf(pyerInf);

        PyeeAcctInfBean pyeeAcctInf = new PyeeAcctInfBean();
        pyeeAcctInf.setPyeeBkId(innerRequest.getPyeeAcctIssrId());
        pyeeAcctInf.setPyeeAcctTp(innerRequest.getPyeeAcctTp());
        if (null == innerRequest.getPyeeSgnNo() || "".equals(innerRequest.getPyeeSgnNo())) {
            pyeeAcctInf.setPyeeBkNo(innerRequest.getPyeeAcctId());
            pyeeAcctInf.setPyeeBkNm(innerRequest.getPyeeNm());
        } else {
            pyeeAcctInf.setPyeeSgnNo(innerRequest.getPyeeSgnNo());
        }
        mchntPaymentAcqRequest.setPyeeAcctInf(pyeeAcctInf);

        PyerAcctInfBean pyerAcctInf = new PyerAcctInfBean();
        pyerAcctInf.setPyerBkId(innerRequest.getResfdAcctIssrId());
        pyerAcctInf.setPyerBkNo(innerRequest.getInstgAcctId());
        pyerAcctInf.setPyerBkNm(innerRequest.getInstgAcctNm());
        mchntPaymentAcqRequest.setPyerAcctInf(pyerAcctInf);

        mchntPaymentAcqRequest.setTrxCtgy(TrxCtgyCdEnum.MCHNT_PAY.getType());
        /*获取网联流水号*/
        NuccSeqNoManage NuccSeqNoManage = new NuccSeqNoManageImpl();
        String epccTxnId = NuccSeqNoManage.genEpccSeqNo();
        mchntPaymentAcqRequest.setTrxId(epccTxnId);
        /*金融交易流水表没有通道流水字段，用remark1来保存*/
        xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.REMARK1, epccTxnId);
        xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.CHANNEL_SEQ_NUM, epccTxnId);
        mchntPaymentAcqRequest.setTrxDtTm(TimeUtil.getDateTime("yyyy-MM-dd'T'HH:mm:ss"));
        mchntPaymentAcqRequest.setTrxAmt(innerRequest.getTrxAmt());
        mchntPaymentAcqRequest.setBizTp(innerRequest.getBizTp());
        mchntPaymentAcqRequest.setTrxPrps(innerRequest.getTrxPrps());
        mchntPaymentAcqRequest.setTrxSmmry(innerRequest.getTrxSmmry());
        mchntPaymentAcqRequest.setTrxUstrd(innerRequest.getTrxUstrd());
        mchntPaymentAcqRequest.setTrxTrmTp(innerRequest.getTrxTrmTp());
        mchntPaymentAcqRequest.setTrxTrmNo(innerRequest.getTrxTrmNo());
        mchntPaymentAcqRequest.setTrxDevcInf(innerRequest.getTrxDevcInf());

        return mchntPaymentAcqRequest;
    }
}
